package Java0309;

import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args) {
        //要求：输出斐波拉切数列的前N项，N由用户输入
        //斐波拉切数列的定义是前两个是1，后面的每一个是前面的两个和，比如N是5,则输出结果是1,1,2,3,5
        Scanner scans = new Scanner(System.in);
        System.out.print("输入数字：");
        int n = scans.nextInt();
        //数列的前两项
        int a = 1, b = 1;
        int c = 0;
        if (n == 1) {
            System.out.println("斐波那契数列的前" + n + "项：");
            System.out.println(a);
        } else if (n == 2) {
            System.out.println("斐波那契数列的前" + n + "项：");
            System.out.println(a + " " + b);
        } else {
            System.out.println("斐波那契数列的前" + n + "项：");
            System.out.print(a + " " + b + " ");
            for (int i = 3; i <= n; i++) {
                c = a + b;
                System.out.print(c + " ");
                a = b;
                b = c;


            }
        }


    }
}

